#include "StdAfx.h"

namespace ui
{

Slider::Slider() : Progress()
{
	m_bMouseWheelEnabled = true;
	m_bMouseClickEnabled = true;
	SetShowThumb(true);
}

#if BUILD_XML_GUI	
void Slider::InitAttrVistor() {

}
std::wstring Slider::GetBuildAttrString(std::wstring sAttr) const {
	return L"";
}
#endif

void Slider::HandleMessage(EventArgs& event)
{
	//if (!IsMouseClickEnabled() && event.Type > kEventMouseBegin && event.Type < kEventMouseEnd) {
	//	if (m_pParent != NULL) m_pParent->HandleMessageTemplate(event);
	//	else Progress::HandleMessage(event);
	//	return;
	//}

	if (event.Type == kEventMouseButtonDown || event.Type == kEventInternalDoubleClick || event.Type == kEventPointDown) {
		if( IsEnabled() ) {
			UiPoint newPtMouse = event.ptMouse;
			newPtMouse.Offset(GetScrollOffset());

			UiRect rcBar = m_rcItem;
			UiRect rcBarMargin = GetBarMargin();
			rcBar.left += rcBarMargin.left;
			rcBar.top += rcBarMargin.top;
			rcBar.right -= rcBarMargin.right;
			rcBar.bottom -= rcBarMargin.bottom;
			int barWidth = m_pProgressBar->GetActureBarWidth();
			if (IsHorizontal())
			{
				if (m_szThumb.cy > barWidth)
					rcBar.top -= (m_szThumb.cy - barWidth) / 2;
				barWidth = max(barWidth, m_szThumb.cy);
				rcBar.bottom = rcBar.top + barWidth;
				assert(m_rcItem.bottom >= rcBar.bottom);		//
			}
			else if (IsVertical())
			{
				if (m_szThumb.cx > barWidth)
					rcBar.left -= (m_szThumb.cx - barWidth) / 2;
				barWidth = max(barWidth, m_szThumb.cx);
				rcBar.right = rcBar.left + barWidth;
				assert(m_rcItem.right >= rcBar.right);		//
			}
			
			if (rcBar.IsPointIn(newPtMouse)) {
				int nValue = m_pProgressBar->CaluValueByGivenPoint(event.ptMouse);
				SetValue(nValue);
				m_bMouseHolder = true;
			}
		}
		return;
	}
	if (event.Type == kEventMouseButtonUp || event.Type == kEventPointUp) {
		if (IsEnabled() && m_bMouseHolder) {
			m_bMouseHolder = false;
			assert(!IsCircle());
			assert(m_pWindow->GetFocus() == this);

			int nValue = m_pProgressBar->CaluValueByGivenPoint(event.ptMouse);
			SetValue(nValue);
		}
		return;
	}
	if (event.Type == kEventMouseScrollWheel) {
		if (IsEnabled())
		{
			int detaValue = event.wParam;
			if (detaValue > 0) {
				SetValue(GetValue() + GetMouseWheelStep());
				return;
			}
			else {
				SetValue(GetValue() - GetMouseWheelStep());
				return;
			}
		}
	}
	if (event.Type == kEventMouseMove || event.Type == kEventPointMove) {
		if (IsEnabled() && m_bMouseHolder) {
			assert(!IsCircle());
			assert(m_pWindow->GetFocus() == this);

			int nValue = m_pProgressBar->CaluValueByGivenPoint(event.ptMouse);
			SetValue(nValue);
			Invalidate();
		}
		return;
	}

	__super::HandleMessage(event);
}

void Slider::SetAttribute(const std::wstring& strName, const std::wstring& strValue)
{
	if (strName == _T("mousewheelstep")) SetMouseWheelStep(_ttoi(strValue.c_str()));
	else __super::SetAttribute(strName, strValue);
}

void Slider::SetValue(int nValue) {
	bool notify = GetValue() != nValue;
	__super::SetValue(nValue);

	if (notify && m_pWindow){
		m_pWindow->SendNotify(this, kEventValueChange);
	}
}

int Slider::GetMouseWheelStep()
{
	return m_nMouseWheelStep;
}

void Slider::SetMouseWheelStep(int step)
{
	m_nMouseWheelStep = step;
}

}
